package com.ruoyi.web.controller.api.shop;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.CharUtil;
import com.ruoyi.system.domain.PStoreProduct;
import com.ruoyi.system.domain.PStoreProductAttrValue;
import com.ruoyi.system.domain.PStoreSeckill;
import com.ruoyi.system.domain.PUserAddress;
import com.ruoyi.system.domain.bo.POrderBo;
import com.ruoyi.system.domain.bo.PStoreReturnBo;
import com.ruoyi.system.domain.bo.PUserAddressBo;
import com.ruoyi.system.domain.vo.PStoreProductVo;
import com.ruoyi.system.domain.vo.PStoreSeckillVo;
import com.ruoyi.system.domain.vo.PUserAddressVo;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: wk
 * @Date: 2022/8/5 11:04
 * @Version: 1.0
 */
@Api(tags = {"前端-商品管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/product")
public class ProductApiController {
    private final IPStoreProductService iPStoreProductService;
    private final IPUserAddressService iPUserAddressService;
    private final IPStoreReturnService iPStoreReturnService;
    private final IPStoreSeckillService seckillService;
    private final IPStoreProductAttrValueService attrValueService;
    private final IPStoreProductRelationService iPStoreProductRelationService;
    private final IPStoreOrderService ipStoreOrderService;

    @ApiOperation("查询商品列表")
    @GetMapping("/list")
    public TableDataInfo<PStoreProduct> list(PStoreProduct bo, PageQuery pageQuery) {
        return iPStoreProductService.queryPageMyList(bo, pageQuery);
    }

    @ApiOperation("获取商品详细信息")
    @GetMapping("/shop/{id}")
    public R<PStoreProductVo> getInfo(@ApiParam("主键")
                                      @NotNull(message = "主键不能为空")
                                      @PathVariable("id") Long id) {
        PStoreProductVo productVo = iPStoreProductService.queryById(id);
        // 判断是否收藏
        boolean flag = iPStoreProductRelationService.queryFlagById(id);
        productVo.setFlag(flag);

        return R.ok(productVo);
    }

    @ApiOperation("查询普通商品规格详情")
    @GetMapping("/attr-value/{id}")
    public R<PStoreProductAttrValue> attrValue(@ApiParam("主键")
                                               @NotNull(message = "商品id不能为空")
                                               @PathVariable("id") Long id) {
        return R.ok(attrValueService.queryFontByProductId(id, "0"));
    }

    @ApiOperation("查询收货人地址列表")
    @GetMapping("/get-user-address")
    public TableDataInfo<PUserAddressVo> list(PUserAddressBo bo, PageQuery pageQuery) {
        bo.setUid(LoginHelper.getUserId());
        return iPUserAddressService.queryPageList(bo, pageQuery);
    }

    @ApiOperation("获取收货人地址详细信息")
    @GetMapping("/address/{id}")
    public R<PUserAddressVo> getAddress(@ApiParam("主键")
                                        @NotNull(message = "主键不能为空")
                                        @PathVariable("id") Long id) {
        return R.ok(iPUserAddressService.queryById(id));
    }

    @ApiOperation("新增收货人地址")
    @PostMapping("/add-address")
    public R<Void> add(@RequestBody PUserAddressBo bo) {
        bo.setUid(LoginHelper.getUserId());
        iPUserAddressService.insertByBo(bo);
        return R.ok();
    }

    @ApiOperation("修改收货人地址")
    @RepeatSubmit()
    @PostMapping("/edit-address")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PUserAddressBo bo) {
        iPUserAddressService.updateByBo(bo);
        return R.ok();
    }

    @ApiOperation("删除收货人地址")
    @GetMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        iPUserAddressService.deleteWithValidByIds(Arrays.asList(ids), true);
        return R.ok();
    }

    @ApiOperation("前端-新增商城订单退款")
    @RepeatSubmit()
    @PostMapping("/add-return")
    public R<Void> addReturn(@Validated(AddGroup.class) @RequestBody PStoreReturnBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        if (ObjectUtil.isEmpty(bo.getProductCount())) {
            bo.setProductCount(1);
        }

        iPStoreReturnService.insertByBo(bo);
        return R.ok();
    }

    @ApiOperation("爆款推荐")
    @GetMapping("/hostList")
    public TableDataInfo<PStoreProduct> hostList(PageQuery pageQuery) {
        return this.iPStoreProductService.hostList(pageQuery);
    }

    @ApiOperation("精品推荐")
    @GetMapping("/goodList")
    public TableDataInfo<PStoreProduct> goodList(PageQuery pageQuery) {
        return this.iPStoreProductService.bestList(pageQuery);
    }

    @ApiOperation("限时秒杀")
    @GetMapping("/seckillList")
    public TableDataInfo<PStoreSeckill> seckillList(PageQuery pageQuery) {
        return this.iPStoreProductService.seckillList(pageQuery);
    }

    @ApiOperation("获取秒杀商品详细信息")
    @GetMapping("/seckill/{id}")
    public R<PStoreSeckillVo> getSeckillInfo(@ApiParam("主键")
                                             @NotNull(message = "主键不能为空")
                                             @PathVariable("id") Long id) {
        return R.ok(seckillService.queryFontById(id));
    }

    @ApiOperation("分类查询")
    @GetMapping("/listByCateId")
    public TableDataInfo<PStoreProduct> listByCateId(@RequestParam("cateId") String cateId, PageQuery pageQuery) {
        return this.iPStoreProductService.listByCateId(cateId, pageQuery);
    }

    @ApiOperation("获取用户默认收货地址")
    @GetMapping("/defaultAddress")
    public R<PUserAddress> defaultAddress() {
        return R.ok(this.iPUserAddressService.defaultAddress());
    }

    @ApiOperation("获取用户收货地址列表")
    @GetMapping("/addressList")
    public R<List<PUserAddress>> addressList() {
        return R.ok(this.iPUserAddressService.addressList());
    }

    @ApiOperation("积分商城列表")
    @GetMapping("/integralProductList")
    public TableDataInfo<PStoreProduct> integralProductList(PageQuery pageQuery) {
        return this.iPStoreProductService.integralProductList(pageQuery);
    }

    @ApiOperation("购物车余额购买")
    @PostMapping("/blanceShop")
    public R<Map<String, String>> blanceShop(@RequestBody POrderBo bo) {
        ipStoreOrderService.blanceShop(bo);
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", bo.getOrderNo());
        return R.ok(map);
    }

    @ApiOperation("积分兑换")
    @PostMapping("/integralShop")
    public R<Map<String, Object>> integralShop(@RequestBody POrderBo bo) {
        bo.setOrderNo(CharUtil.getRandomNum(16));
        ipStoreOrderService.integralShop(bo);
        Map<String, Object> map = new HashMap<>();
        map.put("orderNo", bo.getOrderNo());
        return R.ok(map);
    }
}
